<template>
  <div class="login-container">
    <el-card class="login-card">
      <template #header>
        <h2 class="login-title">港口仓储管理系统</h2>
      </template>
      
      <el-form :model="loginForm" :rules="loginRules" ref="loginFormRef" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item :label-width="'0px'">
          <el-button type="primary" @click="handleLogin" :loading="loading">
            登录
          </el-button>
        </el-form-item>
        <el-form-item :label-width="'0px'">
          <el-button @click="showRegisterDialog">
            注册新用户
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 注册对话框 -->
    <el-dialog v-model="registerDialogVisible" title="用户注册" width="400px">
      <el-form :model="registerForm" :rules="registerRules" ref="registerFormRef" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="registerForm.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="registerForm.password" type="password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="registerForm.confirmPassword" type="password" placeholder="请确认密码" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="registerDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleRegister" :loading="registerLoading">注册</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { login, register } from '@/api/user'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()

const loading = ref(false)
const registerLoading = ref(false)
const registerDialogVisible = ref(false)
const loginFormRef = ref(null)
const registerFormRef = ref(null)

const loginForm = ref({
  username: '',
  password: ''
})

const registerForm = ref({
  username: '',
  password: '',
  confirmPassword: ''
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const registerRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在3到20个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '最少6个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== registerForm.value.password) {
          callback(new Error('两次输入密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

const handleLogin = async () => {
  try {
    await loginFormRef.value.validate()
    loading.value = true
    
    const response = await login(loginForm.value)
    const { token } = response.data
    
    userStore.setToken(token)
    userStore.setUserInfo(response.data.user)
    
    ElMessage.success('登录成功')
    router.push('/in')
  } catch (error) {
    ElMessage.error('登录失败：' + (error.message || '用户名或密码错误'))
  } finally {
    loading.value = false
  }
}

const showRegisterDialog = () => {
  registerForm.value = {
    username: '',
    password: '',
    confirmPassword: ''
  }
  registerDialogVisible.value = true
}

const handleRegister = async () => {
  try {
    await registerFormRef.value.validate()
    registerLoading.value = true
    
    await register({
      username: registerForm.value.username,
      password: registerForm.value.password,
      role: 'CUSTOMER'
    })
    
    ElMessage.success('注册成功，请登录')
    registerDialogVisible.value = false
  } catch (error) {
    ElMessage.error('注册失败：' + (error.message || '注册失败'))
  } finally {
    registerLoading.value = false
  }
}
</script>

<style scoped>
/* 基础容器样式 */
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(to right, #f5f9ff, #e0edff);
}

/* 卡片样式 - 增加深度选择器 */
.login-card {
  width: 400px;
  border-radius: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.login-card ::v-deep .el-card__header {
  border-bottom: 1px solid #ebeef5;
  padding: 18px 20px;
}

.login-title {
  text-align: center;
  color: #409EFF;
  margin: 0;
  font-size: 22px;
  padding-bottom: 0;
}

/* 表单样式 - 使用深度选择器 */
.login-card ::v-deep .el-form {
  padding: 20px 40px;
}

/* 表单项样式 */
.login-card ::v-deep .el-form-item {
  margin-bottom: 22px;
  display: flex;
  align-items: center;
}

/* 标签样式 */
.login-card ::v-deep .el-form-item__label {
  flex: 0 0 80px;
  text-align: right;
  padding-right: 12px;
  color: #606266; /* 明确设置标签颜色 */
  font-weight: normal;
}

/* 输入框样式 */
.login-card ::v-deep .el-form-item__content {
  flex: 1;
}

.login-card ::v-deep .el-input {
  width: 100%;
}

.login-card ::v-deep .el-input__inner {
  color: #606266; /* 确保输入文字颜色 */
}

/* 按钮样式 */
.login-card ::v-deep .el-button {
  width: 100%;
  margin-top: 5px;
}

/* 最后一个表单项 */
.login-card ::v-deep .el-form-item:last-child {
  margin-bottom: 0;
  display: block;
}

/* 对话框样式 */
.login-container ::v-deep .el-dialog__body {
  padding: 20px 30px;
}

/* 卡片悬停效果 */
.login-card {
  transition: all 0.3s ease;
}

.login-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12);
}
</style>